﻿using System;
using System.Collections.Generic;
using System.Text;

namespace deFramework.BusinessRules
{
    /// <summary>
    /// .
    /// </summary>
    public class SpWebListTools : Base.BaseBusinessRule
    {

        #region "Constructor, private Variables"

        /// <summary>
        /// 
        /// </summary>
        /// <param name="aContext"></param>
        public SpWebListTools(ref Common.PresentationContext aContext) : base(ref aContext) { }
        
        #endregion

        #region "public - SetLookupToReferenceTitle, DeleteSharePointList, CreateDocumentLibrary"

        /// <summary>
        /// 
        /// </summary>
        /// <param name="aDataListName"></param>
        /// <param name="aDataReferenceFieldName"></param>
        /// <param name="aReferenceListName"></param>
        /// <returns></returns>
        public bool SetLookupToReferenceTitle(string aDataListName, string aDataReferenceFieldName, string aReferenceListName)
        {

            bool l_Result = false;
            try
            {
                if (this.MyContext.SpWebInContext)
                {
                    this.Do_SetLookupToReferenceTitle(aDataListName, aDataReferenceFieldName, aReferenceListName);
                    l_Result = true;
                }
            }
            catch (Exception ex)
            {
                this.CurrentException = ex;
                //this.mLog.BasicException(ex, "SpWebListTools.SetLookupToReferenceTitle");
            }
            return l_Result;


        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="aListName"></param>
        /// <returns></returns>
        public bool DeleteSharePointList(string aListName)
        {
            bool l_Result = false;
            try
            {
                if (this.MyContext.SpWebInContext)
                {
                    this.Do_DeleteSharePointList(aListName);
                    l_Result = true;
                }
            }
            catch (Exception ex)
            {
                this.CurrentException = ex;
                //this.mLog.BasicException(ex, "SpWebListTools.DeleteSharePointList");
            }
            return l_Result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="aLibraryName"></param>
        /// <returns></returns>
        public bool CreateDocumentLibrary(string aLibraryName)
        {
            bool l_Result = false;
            try
            {
                this.MyContext.MySpWeb.Lists.Add(aLibraryName, "Description for " + aLibraryName,
                    Microsoft.SharePoint.SPListTemplateType.DocumentLibrary);
                l_Result = true;
            }
            catch (Exception ex)
            {
                this.CurrentException = ex;
            }
            return l_Result;
        }


        #endregion


        #region "Private DOs  - SetLookupToReferenceTitle, DeleteSharePointList"

        private void Do_SetLookupToReferenceTitle(string aDataListName, string aDataReferenceFieldName, string aReferenceListName)
        {
            Microsoft.SharePoint.SPList oDataList = this.MyContext.MySpWeb.Lists[aDataListName];
            Microsoft.SharePoint.SPList oRefList = this.MyContext.MySpWeb.Lists[aReferenceListName];
            //Guid oDataGuid = oDataList.ID; 
            Guid oRefGuid = oRefList.ID;
            oDataList.Fields.AddLookup(aDataReferenceFieldName, oRefGuid, false);
            Microsoft.SharePoint.SPFieldLookup oLookupField =
                (Microsoft.SharePoint.SPFieldLookup)oDataList.Fields[aDataReferenceFieldName];
            oLookupField.AllowMultipleValues = false;
            oLookupField.LookupField = "Title";
            oLookupField.Update(true);

        }


        private void Do_DeleteSharePointList(string aListName)
        {
            Microsoft.SharePoint.SPWeb oWeb = this.MyContext.MySpWeb;
            Microsoft.SharePoint.SPList list = oWeb.Lists[aListName];
            list.AllowDeletion = true;
            list.Update();
            this.MyContext.MySpWeb.Lists.Delete(list.ID);
        }


        #endregion

 

    }
}
